<template>
  <v-dialog :value="value" @input="$emit('input', false)">
    <popupVue
      v-if="value"
      @close="$emit('input', false)"
      :columns="columns"
      :filters="filters"
      req_url="/api/RepairReport/ItemPageList"
    >
      <template #title> 报修工单明细 </template>
      <template #sub-title>
        {{ sub_title }}
      </template>
    </popupVue>
  </v-dialog>
</template>

<script>
import popupVue from "./popup.vue";
import { getColumns } from "../../../generate";
import { calcTimeDiffFormatter, distinct } from "../../../utils.js";
import ImgViewPage from "../../../components/Table/ImgView.vue";

export default {
  components: {
    popupVue,
  },
  props: {
    value: Boolean,
    filters: Array,
    sub_title: String,
  },
  data() {
    return {
      columns: [],
    };
  },
  async mounted() {
    let arr = await getColumns("RepairViewListModel");
    this.columns = distinct(
      [
        ...arr,
        {
          Name: "ApplyName",
          Description: "科室/申请人",
          render: (h, r) => {
            let value = ``;
            let { ApplyName, DeptName } = r;

            if (DeptName) {
              value = `${DeptName}/`;
            }
            value += `${ApplyName}`;

            return h(
              "span",
              {
                attrs: {
                  title: value,
                },
              },
              value
            );
          },
        },
        {
          Name: "ApplyTime",
          Description: "申请时间",
          Type: "String",
          render: (h, v) => h("span", null, calcTimeDiffFormatter(v.ApplyTime)),
        },
        {
          Name: "AcceptanceTime",
          Description: "检修时间",
          Type: "String",
          render: (h, v) =>
            h("span", null, calcTimeDiffFormatter(v.AcceptanceTime)),
        },
        {
          Name: "RepairName",
          Description: "工程师(附件)",
          Type: "String",
          render: (h, { Id, RepairName, FileCount }) => {
            let value =
              FileCount > 0
                ? `${RepairName || "待分配"}(${FileCount})`
                : RepairName;

            return h(
              FileCount > 0 ? "a" : "span",
              {
                attrs: {
                  title: "工程师(申请单的附件数)",
                },
                on: {
                  click: async () => {
                    if (FileCount <= 0) return;

                    let fileItem = await this.$http.get(
                      `/api/RepairReport/ResourceItem/${Id}`
                    );

                    this.$message.dialog(ImgViewPage, {
                      width: "1024px",
                      title: "图片预览",
                      defIndex: 0,
                      items: fileItem.map(
                        (v) => `/api/resource/get/${v.Id}/${v.Name}`
                      ),
                    });
                  },
                },
              },
              value
            );
          },
        },
      ],
      (v) => v.Name,
      (a, b) => ({ ...a, ...b })
    );
  },
};
</script>

<style>
</style>